
// FBorgologin.js
$('document').ready(function() {
FBorgo_appid=$('meta[property="fb:app_id"]').attr("content");

});


$('head').append('<div id="fb-root"></div>');
  window.fbAsyncInit = function() {
    FB.init({
      appId      : FBorgo_appid, // App ID
    
      status     : true, // check login status
      cookie     : true, // enable cookies to allow the server to access the session
      xfbml      : true,  // parse XFBML
oauth: true
    });


FB.getLoginStatus(function(response) {


  if (response.status === 'connected') {


$('head').append('<meta property="og:site_name" content="'+location.href+'" />');

// expires=response.authResponse.expiresIn; // può servire per il token o per qualche probl di autenticazione

    // the user is logged in and connected to your
    // app, and response.authResponse supplies
    // the user's ID, a valid access token, a signed
    // request, and the time the access token 
    // and signed request each expire
    uid = response.authResponse.userID;
    accessToken = response.authResponse.accessToken;



var  FB_url = 'https://graph.facebook.com/me?access_token='+accessToken;
$.getJSON(FB_url,function(FBmio){
FBme = FBmio;


var codeauth = location.search.split('code=')[1];

  var cookieBorgo = document.cookie.split('FBorgo=')[1];


  if (!cookieBorgo || codeauth || !FBme.id) {

document.cookie = 'FBorgo='+uid; // solo se il cookie non c'è!!
document.cookie = 'FBorgo_t='+accessToken; // solo se il cookie non c'è!!
document.cookie = 'FBorgo_e='+FBme.email; // solo se il cookie non c'è!!
window.location.reload();
}


if(typeof FB_me == 'function') { 
FB_me();
}


});









var thisfriend = location.search.split('FBorgo_q=')[1];

if (thisfriend) {

idmember = thisfriend.split('&')[0];

var FB_q_url = 'https://graph.facebook.com/'+idmember+'?access_token='+accessToken;
$.getJSON(FB_q_url,
function(FBORGOquery){
FBquery = FBORGOquery;
FB_query();

});

}





var FB_f_url = 'https://graph.facebook.com/me/friends?&access_token='+accessToken;
$.getJSON(FB_f_url,
    function(FBfriend){
FBfriends = FBfriend.data;

if(typeof FB_friends == 'function') { 
FB_friends();
}
if(typeof getfriendmail == 'function') { 
getfriendmail();
}




});





if(typeof FBborgo_custom == 'function') { 
FBborgo_custom();
}





  } else {

// decisione da prendere
$('#user').empty();
$('#admin').empty();
$('#alien').show();




// if (!$('#user') && !$('#admin') && !$('#alien')) {


// ammetti l'accesso PURE (subito login facebook)
window.location.href='https://www.facebook.com/dialog/oauth?client_id='+FBorgo_appid+'&scope=email,read_stream,publish_stream,offline_access,manage_pages&redirect_uri='+window.location.href;

//} else {

// accesso da frame

// }




  }



 });



  };

  // Load the SDK Asynchronously
  (function(d){
     var js, id = 'facebook-jssdk'; if (d.getElementById(id)) {return;}
     js = d.createElement('script'); js.id = id; js.async = true;
     js.src = "//connect.facebook.net/en_US/all.js";
     d.getElementsByTagName('head')[0].appendChild(js);
   }(document));


// <script src=http://borgoweb.com/openborgo/FBorgologin.js></script>


// decisione da prendere
// $('#logged').empty();
// $('#unlogged').show();

// function FB_me();
// $('p').append(FBme.id);
// }


// function FB_query() {
// $('p').append(FBquery.username);
// }



// function FB_friends() {
// $('p').append(FBfriends.length);
// }

// function FBborgo_custom() {
// FBborgo_custom();
// }


//newsletter
// <a id=FBorgo_news href="mailto:?Subject=Oggetto%20della%20mail&Body=Messaggio%20di%20prova">invia a <span id=FB_friendtot></span> amici</a>
//<script src=http://borgoweb.com/openborgo/FBnews.js></script>



////////////PHP///////////////////////
// <?php echo $_COOKIE["FBorgo"] ?>
// <?php echo $_COOKIE["FBorgo_t"] ?>
///////////////////////////////////






